<template>
  <a-modal
    width='618px'
    title='交付物上传'
    cancelText="取消"
    okText="确定"
    :visible="visible"
    @ok="handleOk"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel">
    <commonUpload
      ref="commonUpload"
      :oid='stringOid'
      :multiple='true'
      :fileAddressDefault='fileAddressDefault'
      @selectFileType='selectFileType'
      @selectFileAddress='selectFileAddress'
      @handleUpload='handleUpload'
      @handleRemove='handleRemove'/>
  </a-modal>
</template>
<script>
import {mapGetters} from 'vuex'
import commonUpload from '@/components/common/commonUpload'
import copyACtion from '@/views/singleProject/fileSingle/el-common-js/copy'
export default {
  mixins: [copyACtion],
  data() {
    return {
      stringOid: '',
      visible: false,
      types: '',
      typeDisplayName:'',
      trees: [],
      fileArr: [],
      fileAddressDefault: [],
      confirmLoading: false,
      needFileStart: false,
      oid:JSON.parse(sessionStorage.getItem('oid')),
      lastTreeCheckedKeys:JSON.parse(sessionStorage.getItem('lastTreeCheckedKeys'))
    }
  },
  computed: {
    ...mapGetters({
      'fileSingleData': 'fileSingle'
    })
  },
  watct:{
    lastTreeCheckedKeys(newVal,oldVal){
      this.lastTreeCheckedKeys = newVal
    }
  },
  components: {
    commonUpload
  },
  created() {
    this.stringOid = this.oid + ''
  },
  mounted(){
    
  },
  methods: {
    /**
     * commonUpload 组件回调
     * 功能： props 选择选择文件类型
     */
    selectFileType(props) {
      this.types = props
    },
    /**
     * commonUpload 组件回调
     * 功能： props 选择文件存放位置
     */
    selectFileAddress(props) {
      this.fileAddressDefault = props
      this.trees = props
    },
    /**
     * commonUpload 组件回调
     * 功能： props 选择交付物文件
     */
    handleUpload(info) {
      this.fileArr.push(info)
    },
    /**
     * commonUpload 组件回调
     * 功能：
     *      file 需要删除的文件
     *      fileList 删除之后的文件数组
     */
    handleRemove(file, fileList) {
      // console.log(file)
      // console.log(fileList)
      if(fileList.length <= 0){
          this.fileArr = []
      }else{
          this.fileArr = fileList
      }
      
    },
    /**
     * 确定按钮事件
     * 功能： 上传交付物
     */
    handleOk() {
      let _this = this
      this.trees = this.trees.length < 1 ? this.fileAddressDefault : this.trees
      let rules = {
        '请选择交付物类型....': !this.types,
        '请选择存放位置....': this.trees.length < 1,
        '请选择交付物文件....': this.fileArr.length < 1
      }
      // return
      // this.confirmLoading = true
      this.upFile({
        types: this.types,
        trees: this.trees,
        fileArr: this.fileArr,
        defaultArr: this.fileAddressDefault,
        rules: rules,
        effectKey: 'addFileStart',
        otherObj: {},
        callback: (res) => {
          this.confirmLoading = false
          if (res.code === 404) {
            return false
          }
          if (_this.fileSingleData.needFileStart) {
            if(this.fileArr.length>1){
               _this.cancelData(_this)
               _this.$emit('initTableData')
            }else{
              this.$confirm({
                cancelText: "否",
                okText: "是",
                content: h => <div>文件已上传成功，是否启动文档签审？</div>,
                onOk() {
                  /*_this.fileSingleData.tableArr = res
                  _this.setFileSingle(_this.fileSingleData)*/
                  let newSelectDeliverData = [], newSelectDeliverDataOnce = {}
                  res.map((item) => {
                    newSelectDeliverDataOnce.oid = item['oid']
                    newSelectDeliverDataOnce.type = _this.types
                    newSelectDeliverData.push(newSelectDeliverDataOnce)
                  })
                  let sessArr = {
                    tableArr: newSelectDeliverData
                  }
                  sessionStorage.setItem('fileSingleDataByStart', JSON.stringify(sessArr))
                  _this.cancelData(_this)
                  _this.$router.push('/single/fileSingle/fileStart');
                },
                onCancel() {
                  _this.cancelData(_this)
                  _this.$emit('initTableData')
                },
                class: 'test',
              })
            }
          } else {
            _this.$emit('InitStartProjact', res)
          }
        }
      })
    },
    cancelData(_this) {
      _this.visible = false
      _this.types = ''
        _this.trees = []
        _this.fileArr = []
        _this.fileAddressDefault = []
        _this.$refs.commonUpload.cancel()
    },
    /**
     * 取消按钮事件
     */
    handleCancel() {
      this.visible = false
      this.$refs.commonUpload.cancel()
    },
    /**
     * 开启弹窗
     * key 上级需要开启那个弹窗的key props上级tree选中的tree，propsArr 上级table选中的数据,
     */
    openModal(start, startF) {
      let _this = this
      this.getTreeData({
        callback: (res) => {
          if (res.code === 200) {
            _this.lastTreeCheckedKeys = JSON.parse(sessionStorage.getItem('lastTreeCheckedKeys'))
            _this.initTreeLoc(res.data, _this.lastTreeCheckedKeys)
          }
        }
      })
      this.visible = start
    },
    /**
     * 初始化选择文件存放位置
     */
    initTreeLoc(propsArr, propsitem) {
      console.log(propsArr,propsitem)
      this.fileAddressDefault = []
      propsArr[0].childs.forEach(item => {
        if (item.oid === propsitem.oid) {
          this.fileAddressDefault.push(item.oid)
        } else {
          if (item.childs.length > 0) {
            item.childs.forEach(elItem => {
              if (elItem.oid === propsitem.oid) {
                this.fileAddressDefault.push(item.oid, elItem.oid)
              } else {
                if (elItem.childs.length > 0) {
                  elItem.childs.forEach(el => {
                    if (el.oid === propsitem.oid) {
                      this.fileAddressDefault.push(item.oid, elItem.oid, el.oid)
                    }
                  })
                }
              }
            })
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.width_410 {
  width: 410px;
}
</style>